/* MainWindow.cpp
 *
 * Copyright (C) 2013 Jason Allen
 *
 * This file is part of the Jade Diagram Editor.
 *
 * Jade is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jade is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jade.  If not, see <http://www.gnu.org/licenses/>
 */

#include "MainWindow.h"
#include "AboutDialog.h"
#include "DiagramItemDefaultsWidget.h"
#include "DiagramManager.h"
#include "DiagramToolsWidget.h"
#include "PreferencesDialog.h"
#include <QtSvg>

#include "Diagrams/DiagramLineItem.h"
#include "Diagrams/DiagramBoxItem.h"
#include "Diagrams/DiagramArcItem.h"
#include "Diagrams/DiagramCurveItem.h"
#include "Diagrams/DiagramEllipseItem.h"
#include "Diagrams/DiagramPolylineItem.h"
#include "Diagrams/DiagramPolygonItem.h"
#include "Diagrams/DiagramTextItem.h"

#include "Items/ChartBoxItem.h"
#include "Items/ChartEllipseItem.h"
#include "Items/ChartPlusItem.h"
#include "Items/ChartPolygonItem.h"
#include "Items/ChartSumItem.h"
#include "Items/ElectricItems.h"
#include "Items/LogicItems.h"

//#define RELEASE_BUILD
#undef RELEASE_BUILD

MainWindow::MainWindow(const QString& filePath) : QMainWindow()
{
    Diagram::itemFactory.registerItem(new DiagramLineItem());
    Diagram::itemFactory.registerItem(new DiagramBoxItem());
    Diagram::itemFactory.registerItem(new DiagramArcItem());
    Diagram::itemFactory.registerItem(new DiagramCurveItem());
    Diagram::itemFactory.registerItem(new DiagramEllipseItem());
    Diagram::itemFactory.registerItem(new DiagramPolylineItem());
    Diagram::itemFactory.registerItem(new DiagramPolygonItem());
    Diagram::itemFactory.registerItem(new DiagramTextItem());

	mDefaultUnits = UnitsMils;
	mDefaultWidth = 10000.0;
	mDefaultHeight = 7500.0;
	mDefaultBackgroundBrush = QBrush(Qt::white);
	mDefaultBorderWidth = 24.0;
	mDefaultBorderBrush = QBrush(Qt::black);
	mDefaultGridSize = 100.0;
	mDefaultGridStyle = Diagram::GridGraphPaper;
	mDefaultGridMajorSpacing = 8;
	mDefaultGridMinorSpacing = 2;
	mDefaultGridBrush = QBrush(QColor(0, 128, 128));

    mDiagram = new Diagram();
    mManager = new DiagramManager(mDiagram);
	mManager->setFileInfo("jdm", "Jade Diagram (*.jdm)");

    connect(mManager, SIGNAL(diagramChanged(const QString&)), this, SLOT(updateWindowTitle(const QString&)));
    connect(mManager, SIGNAL(visibilityChanged(bool)), this, SLOT(diagramVisibilityChanged(bool)));
    connect(mManager, SIGNAL(diagramCleared()), this, SLOT(clearDiagram()));

    QFrame* mainFrame = new QFrame();
    QHBoxLayout* mainLayout = new QHBoxLayout();
    mainLayout->addLayout(createToolsWidgets());
    mainLayout->addWidget(mManager, 100);
    mainFrame->setLayout(mainLayout);
    setCentralWidget(mainFrame);

    createActions();
    createMenus();
    createToolBars();
    createStatusBar();
    loadLibraryItems();

    setWindowTitle("Jade");
    setWindowIcon(QIcon(":/icons/custom/diagram.png"));
    resize(1048, 760);

    loadPreferences();
    mManager->loadDiagram(filePath);
}
//--------------------------------------------------------------------------------------------------
MainWindow::~MainWindow() { }
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::savePreferences()
{
#ifdef RELEASE_BUILD
#ifdef WIN32
	QString configPath("config.ini");
#else
	QString configPath(QDir::home().absoluteFilePath(".jade/config.ini"));
#endif
#else
	QString configPath("config.ini");
#endif

	QSettings settings(configPath, QSettings::IniFormat);

	settings.beginGroup("Window");
	settings.setValue("Geometry", saveGeometry());
	settings.setValue("State", saveState());
	settings.endGroup();

	settings.beginGroup("Prompts");
    settings.setValue("PromptOnClosingUnsaved", mManager->shouldPromptOnClosingUnsaved());
    settings.setValue("PromptOnOverwrite", mManager->shouldPromptOnOverwrite());
	settings.endGroup();

	settings.beginGroup("ItemDefaults");
    settings.setValue("Pen", penToString(mItemDefaultsWidget->pen()));
    settings.setValue("Brush", brushToString(mItemDefaultsWidget->brush()));
    settings.setValue("Font", fontToString(mItemDefaultsWidget->font()));
    settings.setValue("TextBrush", brushToString(mItemDefaultsWidget->textBrush()));
    settings.setValue("StartArrowStyle", (int)mItemDefaultsWidget->startArrow().style());
    settings.setValue("StartArrowSize", mItemDefaultsWidget->startArrow().size());
    settings.setValue("EndArrowStyle", (int)mItemDefaultsWidget->endArrow().style());
    settings.setValue("EndArrowSize", mItemDefaultsWidget->endArrow().size());
	settings.endGroup();

	settings.beginGroup("DiagramDefaults");
	settings.setValue("Units", (int)mDefaultUnits);
	settings.setValue("Width", mDefaultWidth);
	settings.setValue("Height", mDefaultHeight);
	settings.setValue("BackgroundColor", mDefaultBackgroundBrush.color());
	settings.setValue("BorderWidth", mDefaultBorderWidth);
	settings.setValue("BorderColor", mDefaultBorderBrush.color());
	settings.setValue("Grid", mDefaultGridSize);
	settings.setValue("GridStyle", (int)mDefaultGridStyle);
	settings.setValue("GridSpacingMajor", mDefaultGridMajorSpacing);
	settings.setValue("GridSpacingMinor", mDefaultGridMinorSpacing);
	settings.setValue("GridColor", mDefaultGridBrush.color());
	settings.endGroup();
}
//--------------------------------------------------------------------------------------------------
void MainWindow::loadPreferences()
{
#ifdef RELEASE_BUILD
#ifdef WIN32
	QString configPath("config.ini");
#else
	QString configPath(QDir::home().absoluteFilePath(".jade/config.ini"));
#endif
#else
	QString configPath("config.ini");
#endif

	QSettings settings(configPath, QSettings::IniFormat);

	if (!settings.allKeys().isEmpty())
	{
		settings.beginGroup("Window");
		restoreGeometry(settings.value("Geometry", QVariant()).toByteArray());
		restoreState(settings.value("State", QVariant()).toByteArray());
        settings.endGroup();

		settings.beginGroup("Prompts");
        mManager->setPromptOnClosingUnsaved(settings.value("PromptOnClosingUnsaved", QVariant(true)).toBool());
        mManager->setPromptOnOverwrite(settings.value("PromptOnOverwrite", QVariant(true)).toBool());
		settings.endGroup();

		settings.beginGroup("ItemDefaults");
        mItemDefaultsWidget->setPen(penFromString(settings.value("Pen", "").toString()));
        mItemDefaultsWidget->setBrush(brushFromString(settings.value("Brush", "").toString()));
        mItemDefaultsWidget->setFont(fontFromString(settings.value("Font", "").toString()));
        mItemDefaultsWidget->setTextBrush(brushFromString(settings.value("TextBrush", "").toString()));
        mItemDefaultsWidget->setStartArrow(DiagramArrow(
			(DiagramArrow::Style)settings.value("StartArrowStyle", DiagramArrow::None).toInt(),
			settings.value("StartArrowSize", 100.0).toReal()));
        mItemDefaultsWidget->setEndArrow(DiagramArrow(
			(DiagramArrow::Style)settings.value("EndArrowStyle", DiagramArrow::None).toInt(),
			settings.value("EndArrowSize", 100.0).toReal()));
		settings.endGroup();
        Diagram::itemFactory.setDefaultProperties(mItemDefaultsWidget->properties());

		settings.beginGroup("DiagramDefaults");
		mDefaultUnits = (DiagramUnits)settings.value("Units", QVariant(UnitsMils)).toInt();
		mDefaultWidth = settings.value("Width", QVariant(10000.0)).toDouble();
		mDefaultHeight = settings.value("Height", QVariant(7500.0)).toDouble();
		mDefaultBackgroundBrush = settings.value("BackgroundColor", QVariant(QColor(255, 255, 255, 255))).value<QColor>();
		mDefaultBorderWidth = settings.value("BorderWidth", QVariant(24.0)).toDouble();
		mDefaultBorderBrush = settings.value("BorderColor", QVariant(QColor(0, 0, 0, 255))).value<QColor>();
		mDefaultGridSize = settings.value("Grid", QVariant(100.0)).toDouble();
		mDefaultGridStyle = (Diagram::GridStyle)settings.value("GridStyle", QVariant(Diagram::GridGraphPaper)).toInt();
		mDefaultGridMajorSpacing = settings.value("GridSpacingMajor", QVariant(8)).toInt();
		mDefaultGridMinorSpacing = settings.value("GridSpacingMinor", QVariant(2)).toInt();
		mDefaultGridBrush = settings.value("GridColor", QVariant(QColor(0, 128, 128, 255))).value<QColor>();
		settings.endGroup();
	}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::preferences()
{
	PreferencesDialog dialog(this);
    dialog.setPrompts(mManager->shouldPromptOnClosingUnsaved(), mManager->shouldPromptOnOverwrite());
	dialog.setUnits(mDefaultUnits);
	dialog.setSize(mDefaultWidth, mDefaultHeight);
	dialog.setBackground(mDefaultBackgroundBrush);
	dialog.setBorder(mDefaultBorderWidth, mDefaultBorderBrush);
	dialog.setGrid(mDefaultGridSize, mDefaultGridStyle, mDefaultGridMajorSpacing,
		mDefaultGridMinorSpacing, mDefaultGridBrush);

	if (dialog.exec() == QDialog::Accepted)
	{
        mManager->setPromptOnClosingUnsaved(dialog.shouldPromptOnClosingUnsaved());
        mManager->setPromptOnOverwrite(dialog.shouldPromptOnOverwrite());

		mDefaultUnits = dialog.units();
		mDefaultWidth = dialog.width();
		mDefaultHeight = dialog.height();
		mDefaultBackgroundBrush = dialog.backgroundBrush();
		mDefaultBorderWidth = dialog.borderWidth();
		mDefaultBorderBrush = dialog.borderBrush();
		mDefaultGridSize = dialog.grid();
		mDefaultGridStyle = dialog.gridStyle();
		mDefaultGridMajorSpacing = dialog.majorGridSpacing();
		mDefaultGridMinorSpacing = dialog.minorGridSpacing();
		mDefaultGridBrush = dialog.gridBrush();
	}
}
//--------------------------------------------------------------------------------------------------
void MainWindow::about()
{
	AboutDialog dialog(this);
	dialog.exec();
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::updateWindowTitle(const QString& fileName)
{
    setWindowTitle(fileName.isEmpty() ? "Jade" : fileName + " - Jade");
}
//--------------------------------------------------------------------------------------------------
void MainWindow::diagramVisibilityChanged(bool visible)
{
    QList<QAction*> lActions = actions();
    QList<QAction*> diagramActions = mDiagram->actions();

    while (!diagramActions.isEmpty())
        diagramActions.takeFirst()->setEnabled(visible);
    lActions[ExportPngAction]->setEnabled(visible);
    lActions[ExportSvgAction]->setEnabled(visible);
    lActions[PrintPreviewAction]->setEnabled(visible);
    lActions[PrintSetupAction]->setEnabled(visible);
    lActions[PrintAction]->setEnabled(visible);
    lActions[PrintPdfAction]->setEnabled(visible);
    mToolsWidget->setEnabled(visible);
    mItemDefaultsWidget->setEnabled(visible);
    mDiagramZoomCombo->setEnabled(visible);
}
//--------------------------------------------------------------------------------------------------
void MainWindow::clearDiagram()
{
	mDiagram->setUnits(mDefaultUnits);
    mDiagram->setSize(mDefaultWidth, mDefaultHeight);
    mDiagram->setBorderWidth(mDefaultBorderWidth);
    mDiagram->setGrid(mDefaultGridSize);
    mDiagram->setGridStyle(mDefaultGridStyle);
    mDiagram->setGridSpacing(mDefaultGridMajorSpacing, mDefaultGridMinorSpacing);
	mDiagram->painter()->setBrushAt(DiagramPainter::Background, mDefaultBackgroundBrush);
	mDiagram->painter()->setBrushAt(DiagramPainter::Border, mDefaultBorderBrush);
	mDiagram->painter()->setBrushAt(DiagramPainter::Grid, mDefaultGridBrush);
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::updateDiagramZoom(const QString& zoomText)
{
	QString zoomTextStr = zoomText;
	QTextStream textStream(&zoomTextStr);
	qreal zoomLevel = 0;

	textStream >> zoomLevel;
	mDiagram->zoomToLevel(zoomLevel / 100.0);
}
//--------------------------------------------------------------------------------------------------
void MainWindow::updateDiagramZoomCombo(qreal zoomLevel)
{
	QString text = QString::number(zoomLevel * 100, 'f', 1) + "%";
	int textIndex = mDiagramZoomCombo->findText(text);

	if (0 <= textIndex && textIndex < mDiagramZoomCombo->count())
		mDiagramZoomCombo->setCurrentIndex(textIndex);
	else
		mDiagramZoomCombo->setEditText(text);
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::showEvent(QShowEvent* event)
{
    QMainWindow::showEvent(event);
    if (!event->spontaneous()) mDiagram->zoomFit();
    else if (!mWindowState.isEmpty()) restoreState(mWindowState);
}
//--------------------------------------------------------------------------------------------------
void MainWindow::hideEvent(QHideEvent* event)
{
    QMainWindow::hideEvent(event);
    if (event->spontaneous()) mWindowState = saveState();
}
//--------------------------------------------------------------------------------------------------
void MainWindow::closeEvent(QCloseEvent* event)
{
    if (mManager->closeDiagram())
    {
        savePreferences();
        event->accept();
    }
    else event->ignore();
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
QLayout* MainWindow::createToolsWidgets()
{
    QVBoxLayout* toolsLayout = new QVBoxLayout();
    QFrame* hSeparator = new QFrame();
    hSeparator->setFrameStyle(QFrame::Sunken | QFrame::HLine);

    mToolsWidget = new DiagramToolsWidget();
    mItemDefaultsWidget = new DiagramItemDefaultsWidget();

    toolsLayout->addWidget(mToolsWidget, 100);
    toolsLayout->addWidget(hSeparator);
    toolsLayout->addWidget(mItemDefaultsWidget);

    connect(mDiagram, SIGNAL(modeReset()), mToolsWidget, SLOT(resetMode()));
    connect(mToolsWidget, SIGNAL(defaultModeClicked()), mDiagram, SLOT(setDefaultMode()));
    connect(mToolsWidget, SIGNAL(scrollModeClicked()), mDiagram, SLOT(setScrollMode()));
    connect(mToolsWidget, SIGNAL(zoomModeClicked()), mDiagram, SLOT(setZoomMode()));
    connect(mToolsWidget, SIGNAL(newModeClicked(const QString&)), mDiagram, SLOT(setNewMode(const QString&)));
    connect(mToolsWidget, SIGNAL(propertiesClicked()), mDiagram, SLOT(properties()));

    return toolsLayout;
}
//--------------------------------------------------------------------------------------------------
void MainWindow::createStatusBar()
{
    mModeLabel = new QLabel("Select Mode");
    mStatusLabel = new QLabel("");
    mNumberOfItemsLabel = new QLabel("0");
    mMouseLabel = new QLabel("");

    mModeLabel->setMinimumWidth(120);
    mStatusLabel->setMinimumWidth(120);
    mNumberOfItemsLabel->setMinimumWidth(80);
    mMouseLabel->setMinimumWidth(120);

    statusBar()->addWidget(mModeLabel);
    statusBar()->addWidget(mStatusLabel);
    statusBar()->addWidget(mNumberOfItemsLabel);
    statusBar()->addWidget(mMouseLabel);

    connect(mDiagram, SIGNAL(modeChanged(const QString&)), mModeLabel, SLOT(setText(const QString&)));
    connect(mDiagram, SIGNAL(cleanChanged(const QString&)), mStatusLabel, SLOT(setText(const QString&)));
    connect(mDiagram, SIGNAL(numberOfItemsChanged(const QString&)), mNumberOfItemsLabel, SLOT(setText(const QString&)));
    connect(mDiagram, SIGNAL(mousePositionChanged(const QString&)), mMouseLabel, SLOT(setText(const QString&)));
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::createActions()
{
    addAction("New...", mManager, SLOT(newDiagram()), ":/icons/oxygen/document-new.png", "Ctrl+N");
    addAction("Open...", mManager, SLOT(openDiagram()), ":/icons/oxygen/document-open.png", "Ctrl+O");
    addAction("Save", mManager, SLOT(saveDiagram()), ":/icons/oxygen/document-save.png", "Ctrl+S");
    addAction("Save As...", mManager, SLOT(saveDiagramAs()), ":/icons/oxygen/document-save-as.png", "Ctrl+Shift+S");
    addAction("Close", mManager, SLOT(closeDiagram()), ":/icons/oxygen/document-close.png", "Ctrl+W");
    addAction("Export PNG...", mManager, SLOT(exportPng()), ":/icons/oxygen/image-x-generic.png");
    addAction("Export SVG...", mManager, SLOT(exportSvg()), ":/icons/oxygen/image-svg+xml.png");
    addAction("Print Preview...", mManager, SLOT(printPreview()), ":/icons/oxygen/document-preview.png");
    addAction("Print Setup...", mManager, SLOT(printSetup()));
    addAction("Print...", mManager, SLOT(printDiagram()), ":/icons/oxygen/document-print.png", "Ctrl+P");
    addAction("Print to PDF...", mManager, SLOT(printPdf()), ":/icons/oxygen/application-pdf.png");
    addAction("Preferences...", this, SLOT(preferences()), ":/icons/oxygen/configure.png");
    addAction("Exit", this, SLOT(close()), ":/icons/oxygen/application-exit.png");

    addAction("About...", this, SLOT(about()), ":/icons/oxygen/help-about.png");
    addAction("About Qt...", qApp, SLOT(aboutQt()));
}
//--------------------------------------------------------------------------------------------------
void MainWindow::createMenus()
{
    QList<QAction*> lActions = actions();
    QList<QAction*> diagramActions = mDiagram->actions();
    QMenu* menu;

    menu = menuBar()->addMenu("File");
    menu->addAction(lActions[NewAction]);
    menu->addAction(lActions[OpenAction]);
    menu->addSeparator();
    menu->addAction(lActions[SaveAction]);
    menu->addAction(lActions[SaveAsAction]);
    menu->addSeparator();
    menu->addAction(lActions[CloseAction]);
    menu->addSeparator();
    menu->addAction(lActions[ExportPngAction]);
    menu->addAction(lActions[ExportSvgAction]);
    menu->addSeparator();
    menu->addAction(lActions[PrintPreviewAction]);
    menu->addAction(lActions[PrintSetupAction]);
    menu->addAction(lActions[PrintAction]);
    menu->addAction(lActions[PrintPdfAction]);
    menu->addSeparator();
    menu->addAction(lActions[PreferencesAction]);
    menu->addSeparator();
    menu->addAction(lActions[ExitAction]);

    menu = menuBar()->addMenu("Edit");
    menu->addAction(diagramActions[Diagram::UndoAction]);
    menu->addAction(diagramActions[Diagram::RedoAction]);
    menu->addSeparator();
    menu->addAction(diagramActions[Diagram::CutAction]);
    menu->addAction(diagramActions[Diagram::CopyAction]);
    menu->addAction(diagramActions[Diagram::PasteAction]);
    menu->addSeparator();
    menu->addAction(diagramActions[Diagram::SelectAllAction]);
    menu->addAction(diagramActions[Diagram::SelectNoneAction]);

    menu = menuBar()->addMenu("Object");
    menu->addAction(diagramActions[Diagram::RotateAction]);
    menu->addAction(diagramActions[Diagram::RotateBackAction]);
    menu->addAction(diagramActions[Diagram::FlipAction]);
    menu->addAction(diagramActions[Diagram::DeleteAction]);
    menu->addSeparator();
    menu->addAction(diagramActions[Diagram::GroupAction]);
    menu->addAction(diagramActions[Diagram::UngroupAction]);
    menu->addSeparator();
    menu->addAction(diagramActions[Diagram::BringForwardAction]);
    menu->addAction(diagramActions[Diagram::SendBackwardAction]);
    menu->addAction(diagramActions[Diagram::BringToFrontAction]);
    menu->addAction(diagramActions[Diagram::SendToBackAction]);

    menu = menuBar()->addMenu("Diagram");
    menu->addAction(diagramActions[Diagram::PropertiesAction]);
    menu->addSeparator();
    menu->addAction(diagramActions[Diagram::ZoomInAction]);
    menu->addAction(diagramActions[Diagram::ZoomOutAction]);
    menu->addAction(diagramActions[Diagram::ZoomFitAction]);

    menu = menuBar()->addMenu("Help");
    menu->addAction(lActions[AboutAction]);
    menu->addAction(lActions[AboutQtAction]);
}
//--------------------------------------------------------------------------------------------------
void MainWindow::createToolBars()
{
    QList<QAction*> lActions = actions();
    QList<QAction*> diagramActions = mDiagram->actions();

    mDiagramZoomCombo = new QComboBox();
    for(int i = 0; i < Diagram::kZoomLevels.size(); i++)
        mDiagramZoomCombo->addItem(QString::number(Diagram::kZoomLevels[i] * 100) + "%");
    mDiagramZoomCombo->setEditable(true);
    connect(mDiagramZoomCombo, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(updateDiagramZoom(const QString&)));
    connect(mDiagram, SIGNAL(zoomLevelChanged(qreal)), this, SLOT(updateDiagramZoomCombo(qreal)));

    QFontMetrics fontMetrics(mDiagramZoomCombo->font());
    mDiagramZoomCombo->setMinimumWidth(fontMetrics.width("8888.8888%") + 16);

    mFileToolBar = new QToolBar("File");
    mFileToolBar->setObjectName("FileToolBar");
    mFileToolBar->setIconSize(QSize(16, 16));
    mFileToolBar->addAction(lActions[NewAction]);
    mFileToolBar->addAction(lActions[OpenAction]);
    mFileToolBar->addAction(lActions[SaveAction]);
    mFileToolBar->addAction(lActions[CloseAction]);
    mFileToolBar->addAction(lActions[PrintPreviewAction]);
    mFileToolBar->addAction(lActions[PrintAction]);
    addToolBar(mFileToolBar);

    mDiagramToolBar = new QToolBar("Diagram");
    mDiagramToolBar->setObjectName("DiagramToolBar");
    mDiagramToolBar->setIconSize(QSize(16, 16));
    mDiagramToolBar->addAction(diagramActions[Diagram::RotateAction]);
    mDiagramToolBar->addAction(diagramActions[Diagram::RotateBackAction]);
    mDiagramToolBar->addAction(diagramActions[Diagram::FlipAction]);
    mDiagramToolBar->addSeparator();
    mDiagramToolBar->addAction(diagramActions[Diagram::ZoomInAction]);
    mDiagramToolBar->addWidget(mDiagramZoomCombo);
    mDiagramToolBar->addAction(diagramActions[Diagram::ZoomOutAction]);
    mDiagramToolBar->addAction(diagramActions[Diagram::ZoomFitAction]);
    addToolBar(mDiagramToolBar);
}
//--------------------------------------------------------------------------------------------------
QAction* MainWindow::addAction(const QString& text, QObject* slotObj,
    const char* slotFunction, const QString& iconPath, const QString& shortcut)
{
    QAction* action = new QAction(text, this);
    connect(action, SIGNAL(triggered()), slotObj, slotFunction);

    if (!iconPath.isEmpty()) action->setIcon(QIcon(iconPath));
    if (!shortcut.isEmpty()) action->setShortcut(QKeySequence(shortcut));

    QMainWindow::addAction(action);
    return action;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void MainWindow::loadLibraryItems()
{
	QList<DiagramItem*> chartItems;

	// Chart items
	chartItems.append(new ChartBoxItem());
	chartItems.append(new ChartEllipseItem());
	chartItems.append(new ChartPolygonItem());
	chartItems.append(new ChartPlusItem());
	chartItems.append(new ChartSumItem());
	addLibrary("Chart", chartItems);

	// Path items
	addLibrary("Electric", ElectricItem::create());
	addLibrary("Logic", LogicItem::create());
}
//--------------------------------------------------------------------------------------------------
void MainWindow::addLibrary(const QString& name, const QList<DiagramItem*>& items)
{
    for(int i = 0; i < items.size(); i++)
        Diagram::itemFactory.registerItem(items[i]);

    mToolsWidget->addLibrary(name, items);
}
//--------------------------------------------------------------------------------------------------
